package com.andreas.controller;

import com.andreas.pojo.*;
import com.andreas.service.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author andreaszhou
 * @ClassName CategoryController
 * @Description TODO
 * @date 2021/1/19 19:02
 * @Version 1.0
 */
@Controller
public class BookController {
    @Resource
    private CategoryService categoryServiceImp;
    @Resource
    private BookService bookServiceImp;
    @Resource
    private DetailService detailServiceImp;
    @Resource
    private EvaluationService evaluationServiceImp;
    @Resource
    private MemberService memberServiceImp;

    @GetMapping("/")
    public ModelAndView showIndex(){
        /*
        显示首页
         */
        ModelAndView modelAndView = new ModelAndView("/index");
        List<Category> categories = categoryServiceImp.showCategoryService();
        modelAndView.addObject("categoryList",categories);
        return modelAndView;
    }

    /*
    * 查询首页图书
    * */
    @GetMapping("/books")
    @ResponseBody
    public IPage<Book> showBooks(@RequestParam("categoryId") Integer categoryId,@RequestParam("order") String order,@RequestParam("page") Integer page){
        IPage<Book> iPage = bookServiceImp.showBookService(categoryId,order,page,6);
        return iPage;
    }

    /*
    * 查询图书详情
    * */
    @GetMapping("/book/{id}")
    public ModelAndView ShowDetailBook(@PathVariable("id") int id, HttpSession session){
        ModelAndView modelAndView = new ModelAndView("/detail");
        Book book = detailServiceImp.showBookDetailService(id);
        List<Evaluation> evaluationList = evaluationServiceImp.showEvaluationService(book.getBookId());
        Member member = (Member)session.getAttribute("member");
        if (member!=null){
            MemberReadState memberReadState = memberServiceImp.selectMemberReadState(member.getMemberId(), id);
            modelAndView.addObject("memberReadState",memberReadState);
        }
        modelAndView.addObject("book",book);
        modelAndView.addObject("evaluationList",evaluationList);
        return modelAndView;
    }
}
